import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 有一个序列 a[1], a[2], ..., a[n]
 * 每次可以交换相邻的两个元素，代价为两个元素中较大的那个
 * 要通过交换将序列变为从小到大递增的序列，总代价最少为多少
 * 样例输入
 * 4
 * 1 5 2 1
 * 样例输出
 * 12
 */
public class Test7 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] num = new int[n];
        for (int i = 0; i < n; i++) {
            num[i] = sc.nextInt();
        }
        long count = 0;
        for (int i = 0; i < n; i++) {
            boolean flg = false;
            for (int j = 0; j < n - i - 1; j++) {
                if(num[j] > num[j + 1]) {
                    count += num[j];
                    int tmp = num[j];
                    num[j] = num[j + 1];
                    num[j + 1] = tmp;
                    flg = true;
                }
            }
            if(!flg) {
                break;
            }
        }
        System.out.println(count);
    }
}
